#!/usr/bin/env python
#
#       Ships.py
#       
#       Copyright 2009 Ben Davis <its.hip@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import Angles
import Types
import Weapons

class Ships(Types.UIDDict):
    pass
        
class Ship:
    def __init__(self, player, x, y, r, type="Missingno"):
        self.player = player.key()
        self.x = x
        self.y = y
        self.r = Angles.Angle(r)
        self.health = self.health_max
        self.type=type
        
    def key(self):
        return self.__key__
    
    def pos(self):
        return self.x, self.y, self.r
        
    def get_direction_to_tile(self, x, y):
        return self.get_direction_to_tile_relative(x - self.x, y - self.y)
        
    def get_direction_to_tile_relative(self, x, y):
        i = 0
        for pos in (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1):
            if pos == (x, y):
                return Angles.Angle(i)
            i += 1
            
    def get_direction_relative(self, direction):
        # gets a direction relative to the ships direction. e.g. 2 = right, so this will get you right of the ship.
        return self.r + direction
        
    def get_relative_tile_direction_relative(self, x, y, direction):
        r = self.get_direction_relative(direction)
        if r == 0: # N
            return x, -y
        elif r == 2: # E
            return y, x
        elif r == 4: # S
            return -x, y
        elif r == 6: # W
            return -y, -x
        elif r == 1: # NE
            rx, ry = y + x, -y + x
        elif r == 3: # SE
            rx, ry = y - x, y + x
        elif r == 5: # SW
            rx, ry = -y - x, y - x
        elif r == 7: # NW
            rx, ry = -y + x, -y - x
        if abs(rx) != abs(ry):
            rx = rx / 2
            ry = ry / 2
        return rx, ry
    
    def get_tile_direction_relative(self, x, y, direction):
        x, y = self.get_relative_tile_direction_relative(x, y, direction)
        return self.x + x, self.y + y
    
    def poll(self):
        """Run at the start of ship's player's turn"""
        self.steam += self.steam_prod
        if self.steam > self.steam_max:
            self.steam = self.steam_max

'''class Pawn(Ship):
    def __init__(self, player, x, y, r):
        self.weapons = (Weapons.PawnCannonA(), Weapons.PawnCannonB())
        self.steam = 6
        self.steam_max = 18
        self.steam_prod = 6
        self.health_max = 10
        self.r_per_turn = 2
        self.r_remaining = 2
        self.r_amount_max = 3
        self.r_amount_remaining = 3
        self.move_remaining = 6
        self.move_max = 6
        Ship.__init__(self, player, x, y, r, "Pawn")'''
class Pawn(Ship):
    def __init__(self, player, x, y, r):
        self.weapons = (Weapons.PawnCannonA(), Weapons.PawnCannonB())
        self.steam = 600
        self.steam_max = 1800
        self.steam_prod = 600
        self.health_max = 10
        self.r_per_turn = 200
        self.r_remaining = 200
        self.r_amount_max = 300
        self.r_amount_remaining = 300
        self.move_remaining = 600
        self.move_max = 600
        Ship.__init__(self, player, x, y, r, "Pawn")

    
class MissingNo(Ship):
    def __init__(self, player, x, y, r):
        self.weapons = (Weapons.PawnCannonA(), Weapons.PawnCannonB())
    pass

class Knight(Ship):
    def __init__(self, player, x, y, r):
        self.weapons = (Weapons.PawnCannonA(), Weapons.PawnCannonB())
        self.steam = 6
        self.steam_max = 18
        self.steam_prod = 6
        self.health_max = 20
        self.r_per_turn = 2
        self.r_remaining = 2
        self.r_amount_max = 3
        self.r_amount_remaining = 3
        self.move_remaining = 6
        self.move_max = 6
        Ship.__init__(self, player, x, y, r, "Knight")
        
class King(Ship):
    def __init__(self, player, x, y, r):
        self.weapons = (Weapons.KingCannonS(), Weapons.KingCannonW()) #water cannon when coded lulz.
        self.steam = 6
        self.steam_max = 24
        self.steam_prod = 4
        self.health_max = 20
        self.r_per_turn = 2
        self.r_remaining = 2
        self.r_amount_max = 3
        self.r_amount_remaining = 3
        self.move_remaining = 3
        self.move_max = 3
        Ship.__init__(self, player, x, y, r, "King")
